package com.example.administrator.adroidtest;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import api.WiFiSensorManager;
import parm.MDF;
import parm.MeasurementParam;
import sensor.Sensor;

public class MeasurementParamAddActivity extends Activity {

    public MeasurementParam mp = new MeasurementParam();
    public MDF mdfa, mdfs, mdfd, mdfe;

    /**
     * 传感器对象
     */
    private Sensor sensor = null;

    private WiFiSensorManager mgr = null;

    private int pos = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_measurement_param_add);

    //    init();
    }

    public void onClickAcceleratedspeed(View view) {
        Intent intent = new Intent(this, AcceleratedspeedActivity.class);
        startActivityForResult(intent, 1);
    }

    public void onClickSpeed(View view) {
        Intent intent = new Intent(this, SpeedActivity.class);
        startActivityForResult(intent, 2);
    }

    public void onClickDisplacement(View view) {
        Intent intent = new Intent(this, DisplacementActivity.class);
        startActivityForResult(intent, 3);
    }

    public void onClickEnvelope(View view) {
        Intent intent = new Intent(this, EnvelopeActivity.class);
        startActivityForResult(intent, 4);
    }

    public void onClickSavebt(View view){
        /**
         * 先提交数据
         */
        submit();
        Intent intent=new Intent(this,MeasurementParamListActivity.class);
        Bundle bundle=new Bundle();
      //  System.out.println("111111111111");
        intent.putExtras(bundle);
        setResult(ResultCode.RESUTLT_OK,intent);
        finish();
    }

    /**
     * 初始化传感器对象
     */
    private void init(){
        mgr = WiFiSensorManager.getInstance();
        //获取bundle
        Intent intent = this.getIntent();
        pos = intent.getIntExtra("pos",-1);
        if(pos == -1){
            //QuickToast.showCannoteFindSensor(this);
            //this.finish();
        }
        //获取传感器对象
        try {
            sensor = mgr.getSensorList().get(pos);
        }catch(IndexOutOfBoundsException e){
            //QuickToast.showCannoteFindSensor(this);
            e.printStackTrace();
            //this.finish();
        }
    //    finish();
    }



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (resultCode) {
            case ResultCode.RESA_OK:
                Bundle bundle = data.getExtras();
                mdfa = (MDF) bundle.getSerializable("mdfa");
                Toast.makeText(getApplicationContext(), "zz" + mdfa.getCharacterValueType(), Toast.LENGTH_SHORT).show();
                break;
            case ResultCode.RESS_OK:
                Bundle bundlespeed = data.getExtras();
                mdfs = (MDF) bundlespeed.getSerializable("mdfs");
                Toast.makeText(getApplicationContext(), "ss" + mdfs.getCharacterValueType(), Toast.LENGTH_SHORT).show();
                break;
            case ResultCode.RESE_OK:
                Bundle bundleenvelope = data.getExtras();
                mdfe = (MDF) bundleenvelope.getSerializable("mdfe");
                Toast.makeText(getApplicationContext(), "ee" + mdfe.getCharacterValueType(), Toast.LENGTH_SHORT).show();
                break;
            case ResultCode.RESD_OK:
                Bundle bundledisplacement = data.getExtras();
                mdfd = (MDF) bundledisplacement.getSerializable("mdfd");
                Toast.makeText(getApplicationContext(), "dd" + mdfd.getCharacterValueType(), Toast.LENGTH_SHORT).show();
                break;
            default:
                Toast.makeText(getApplicationContext(), "no data come back!", Toast.LENGTH_SHORT).show();
                break;
        }

    }

    /**
     * 对返回来的数据进行添加到一个总体MeasurementParamManager
     */
    private void submit() {
        int index = 0;
        if (null != mdfa)
            index++;
        if (null != mdfs)
            index++;
        if (null != mdfd)
            index++;
        if (null != mdfe)
            index++;

        /**
         * 如果没有数据需要提交就不进入下面提交总的MeasurementParaddd
         */
        if (index != 0) {
            MDF[] mdfss = new MDF[index+1];
            for(int i=0;i<=index;i++){
                if(null!=mdfa) {
                    mdfss[i] =mdfa;
                    continue;
                }
                if(null!=mdfs){
                    mdfss[i]=mdfs;
                    continue;
                }
                if(null!=mdfd){
                    mdfss[i]=mdfd;
                    continue;
                }
                if(null!=mdfe){
                    mdfss[i]=mdfe;
                    continue;
                }
            }
     //       System.out.println(mdfss);
            mp.setMdfArray(mdfss);
       //     System.out.println(mp.getMdfArray().length);
            MeasurementParamManager.getInstance().getMeasurementparamslist().add(mp);
        }

    }


}
